#!/bin/sh

#################################################################
#  This program is the Confidential and Proprietary product of 
#  Cadence Design Systems.  Any unauthorized use, reproduction 
#  or transfer of this program is strictly prohibited. 
#  
#  Copyright (c) 1989 - 1995, Cadence Design Systems, Inc. 
#  All rights reserved. 
#  
#  Filename:  liban
#  Purpose:   Analyzes the library
#  Author:    naji@cds9255.cadence.com
#  Date:      January 1995
#  
#  History:   
#################################################################

program=`basename $0`
version="1.0"

libfile="/tmp/$program.$$.v"

trap '/bin/rm -f $libfile 2>/dev/null' 0 1 2 3 15 16 17

#
# define procedure to display usage message and exit
#

usage_exit() {

cat << EOF >&2
Usage: $program [-help] libname

Description: Analyzes a library using VAN, the Verilog
Analyzer. The library path is extracted from the cds.lib
file in the current working directory.
EOF

exit ${1:-1}
}

#
# print program and copyright information
#

echo "Running $program version $version - `date`."
echo "Copyright (c) 19`date +%y` Cadence Design Systems."
echo "All Rights Reserved."
echo ""

#
# process command line arguments
#

while [ $# -gt 0 ]; do

	case $1 in

		-help)  usage_exit 0 ;;

		-*)     echo "Error: unrecognized option '$1'" >&2
				echo "" >&2
		        usage_exit ;;

		*)      library=$1 ; shift ;;
	esac

done

if [ ! "$library" ]; then
	echo "Error: Must specify a library name !" >&2
	echo "" >&2
	usage_exit
fi

#
# get the 5.X library path
#

if [ ! -r "cds.lib" ]; then
	echo "Error: Cannot find or read file - cds.lib" >&2
	exit 1
fi

libpath=`awk '/^define / { if ( $2 == lib ) print $3 }' lib=$library cds.lib`

if [ -z "$libpath" ]; then
	echo "Error: Cannot find library '$library' in cds.lib" >&2
	exit 1
fi

#
# compile library Verilog modules into one file
#

echo "Compiling the library ..."

if [ ! -d "$libpath" ]; then
	echo "" >&2
	echo "Error: Cannot access library - $library" >&2
	exit 1
fi

/bin/rm -f $libfile

for cell in `/bin/ls $libpath`; do

	if [ -f $libpath/$cell/verilog_lib/verilog.v ]; then

		cat $libpath/$cell/verilog_lib/verilog.v >> $libfile 2>&1

		if [ $? != 0 ]; then
			echo "" >&2
			echo "Error: Failed to compile library - $library" >&2
			exit 1
		fi
	fi

done

#
# run Verilog Analyzer on library Verilog file
#

echo ""

van -lib $library $libfile

if [ $? != 0 ]; then
	echo "" >&2
	echo "Error: Failed to analyze library - $library" >&2
	exit 1
fi

echo ""

#
# update the analyzed views' Verilog source file link
#

echo "Updating links ..."

for cell in `/bin/ls $libpath`; do

	if [ -f $libpath/$cell/hdl/verilog.v ]; then

		/bin/rm -f $libpath/$cell/hdl/verilog.v

		ln -s ../verilog_lib/verilog.v $libpath/$cell/hdl/verilog.v 2>&1

		if [ $? != 0 ]; then
			echo "" >&2
			echo "Error: Failed to update links in library - $library" >&2
			exit 1
		fi
	fi

done

#
# normal exit
#

echo ""
echo "Done."
exit 0
